package com.amiu.mybatisWithXml;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.amiu.Bean;

public class SimpleMyBatisTest {
	
	@Test
	public void findById() throws IOException{
		InputStream is = null;
		SqlSession sqlSession = null;
		try {
			is = Resources.getResourceAsStream("com/amiu/mybatisWithXml/mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = 
					new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			Bean bean = sqlSession.selectOne("test.findById",1);
			
			System.out.println(bean.getId());
			System.out.println(bean.getName());
			System.out.println(bean.getPassword());
			System.out.println(bean.getPhone());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}finally{
			if( sqlSession!=null)
				sqlSession.close();
		}
	}
	
	//@Test
	public void findByName(){
		InputStream is = null;
		SqlSession sqlSession=null;
		try {
			is = Resources.getResourceAsStream("com/amiu/mybatisWithXml/mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = 
					new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			List<Bean> beansList = sqlSession.selectList("test.findByName","hellomini");
			for (Bean bean : beansList) {
				System.out.println(bean.getId());
				System.out.println(bean.getName());
				System.out.println(bean.getPassword());
				System.out.println(bean.getPhone());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
	}
	
	//@Test
	public void insert(){
		InputStream is = null;
		SqlSession sqlSession = null;
		
		try {
			is = Resources.getResourceAsStream("com/amiu/mybatisWithXml/mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = 
					new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			Bean test = new Bean();
			//test.setId(4);
			test.setName("beanName");
			test.setPassword("beanPwd1");
			test.setPhone("130000001");
			sqlSession.insert("test.insert",test);
			sqlSession.commit();
			System.out.println(test.getId());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
		
	}
	
	//@Test
	public void deleteById(){
		InputStream is = null;
		SqlSession sqlSession = null;
		
		try {
			is = Resources.getResourceAsStream("com/amiu/mybatisWithXml/mybatis-config.xml");
			
			SqlSessionFactory sqlSessionFactory = 
					new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			sqlSession.delete("test.deleteById",5);
			sqlSession.commit();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
	}

	//@Test
	public void updateById(){
		InputStream is = null;
		SqlSession sqlSession=null;
		try {
			is = Resources.getResourceAsStream("com/amiu/mybatisWithXml/mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = 
					new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			Bean bean = new Bean();
			bean.setId(4);
			bean.setName("updateName");
			sqlSession.update("test.updateById",bean);
			sqlSession.commit();
		} catch (IOException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
	}
	
}
